﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Application.AppLogManage;

namespace Zocono.WCS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class JobInfoController : ControllerBase
    {
        private readonly ILogUtil _LogUtil;
        private readonly JobInfoApp _JobInfoApp;
        private readonly LogApp _LogApp;

        public JobInfoController(ILogUtil logUtil, JobInfoApp jobInfoApp, LogApp logApp)
        {
            _LogUtil = logUtil;
            _JobInfoApp = jobInfoApp;
            _LogApp = logApp;
        }

        [HttpPost]
        public async Task<HeaderResult<List<JobInfo>>> GetPagedJobInfoAsync(QueryJobInfoDto queryJobInfo)
        {
            HeaderResult<List<JobInfo>> headerResult = new HeaderResult<List<JobInfo>>();
            try
            {
                PageDto<JobInfo> pageDto = await _JobInfoApp.GetPagedJobInfoAsync(queryJobInfo);
                headerResult.IsSucceed = true;
                headerResult.Result = pageDto.Result;
                headerResult.Total = pageDto.Total;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpGet]
        public async Task<HeaderResult<List<JobInfo>>> GetJobInfoListAsync()
        {
            InterfaceLogInfo log = null;
            HeaderResult<List<JobInfo>> headerResult = new HeaderResult<List<JobInfo>>();
            try
            {
                var result = await _JobInfoApp.GetJobInfoListAsync();
                headerResult.IsSucceed = true;
                headerResult.Result = result;
                headerResult.Message = "查询成功";
                //TODO临时测试
                await _LogApp.AddInterfaceLogAsync(log = new()
                {
                    InterfacePara = "Test",
                    InterfaceResult = "成功",
                    InterfaceUrl = "GetJobInfoListAsync",
                    Description = "测试Quartz的调用",
                    ActionName = "GetJobInfoListAsync",
                    IsSuccess = true,
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now,

                });
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> CreateJobInfoAsync(JobInfo jobInfo)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _JobInfoApp.CreateJobInfoAsync(jobInfo);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "新增成功" : "新增失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> ModifyJobInfoAsync(JobInfo jobInfo)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _JobInfoApp.ModifyJobInfoAsync(jobInfo);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "修改成功" : "修改失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> DeleteJobInfoAsync(List<JobInfo> jobInfoList)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _JobInfoApp.DeleteJobInfoAsync(jobInfoList);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "删除成功" : "删除失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }
    }
}
